package team.shixi.houtai.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import team.shixi.houtai.common.Result;
import team.shixi.houtai.entity.Admin;
import team.shixi.houtai.service.AdminService;

import javax.servlet.http.HttpServletRequest;


@RestController
@RequestMapping("/login")
public class AdminController {

    @Autowired
    private AdminService adminService;

    /*@PutMapping
    public Result<?> update(@RequestBody Admin admin){
        LambdaQueryWrapper<Admin> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Admin::getUsername,admin.getUsername());
        adminService.getOne()
    }*/

    @PostMapping
    public Result<?> login(@RequestBody Admin admin, HttpServletRequest request){
        LambdaQueryWrapper<Admin> queryWrapper = new LambdaQueryWrapper<>();
        //使用用户名判断，因为数据库已经添加用户名唯一性

        queryWrapper.eq(Admin::getUsername,admin.getUsername());
        Admin one = adminService.getOne(queryWrapper);

        if (one == null){
            //未查询到
            return Result.error("用户名错误！");
        }
        if (!one.getPassword().equals(admin.getPassword())){
            //密码错误
            return Result.error("密码错误！");
        }
        //登录成功
        request.setAttribute("admin",admin);
        return Result.success();

    }
}
